<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
	exit ('Access Denied');
}

/**
 * 临时文件管理业务逻辑类
 * 
 * 每个 Logic 对应一个业务逻辑处理单元、必须继承自 BaseLogic
 * 每个 Logic 对应一个或多个 View/DAO
 */
class ThumbLogic extends TinBaseLogic {

	private $thumbDao;

	// 每个 Logic 需要有一个构造函数
	public function ThumbLogic() {
		parent::__construct();
		$this->logger->className = "ThumbLogic";
		$this->thumbDao = Tin::getDAO("AssignTable","THUMB");
	}
	
	public function __destruct(){
		$this->thumbDao = null;
	}
	
	/** 获取指定临时文件记录 */
	public function getThumbItem($cols, $cond){
		return $this->thumbDao->selectRowByCond($cols, $cond);
	}
	/** 根据条件获取临时文件列表 */
	public function getThumbList($cols, $cond, $order_by  = null, $page = null, $size = null){
		$res["data"] = $this->thumbDao->selectByCond($cols, $cond . " " . $order_by, $page, $size);
		if ($page){
			$res["count"] = $this->thumbDao->getCountByCond($cond);
		}
		return $res;
	}
	/** 添加临时文件记录 */
	public function insertThumb($data){
		$map = Tin :: createDataMap();		
		$map->addEntry('USER_ID', $data['user_id'] ? $data['user_id'] : 0, DB::INT);
		if($data['good_id']) $map->addEntry('GOODS_ID', $data['good_id'], DB::INT);
		if($data['pro_id']) $map->addEntry('PRODUCTS_ID', $data['pro_id'], DB::INT);
		$map->addEntry('THUMB_URL', $data['url'], DB::VARCHAR);
		$map->addEntry('THUMB_SIZE', $data['size'], DB::INT);
		$map->addEntry('CREATE_TIME', 'now()', DB::DATETIME);
		try{
			$res = $this->thumbDao->insert($map, false, true);
			return $res;
		}catch(Exception $e){
			$this->logger->error($e->getMessage());
			return null;
		}
	}
	/** 更新指定临时文件记录 */
	public function updateThumb($data, $cond){
		$map = Tin :: createDataMap();
		$map->addEntry('USER_ID', $data['user_id'] ? $data['user_id'] : 0, DB::INT);
		if($data['good_id']) $map->addEntry('GOODS_ID', $data['good_id'], DB::INT);
		if($data['pro_id']) $map->addEntry('PRODUCTS_ID', $data['pro_id'], DB::INT);
		$map->addEntry('THUMB_URL', $data['url'], DB::VARCHAR);
		$map->addEntry('THUMB_SIZE', $data['size'], DB::INT);
		try{
			$res = $this->thumbDao->updateByCond($map, $cond, false);
			return $res;
		}catch(Exception $e){
			$this->logger->error($e->getMessage());
			return null;
		}
	}
	/** 删除指定临时文件记录 */
	public function delThumb($cond, $params = false){
		//如果有资源服务器，调用资源服务器删除文件接口
		return $this->thumbDao->deleteByCond($cond, $params);
	}

}//End Class
?>
